package dao.Impl;

import dao.IntroDuctDao;
import dto.IntroDuctModelDto;
import model.IntroDuctModel;
import util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class IntroDuctImpl implements IntroDuctDao {
    @Override
    public int add(IntroDuctModelDto introDuctModelDto) {
        String sql ="INSERT INTO `hs_introduct` " +
                "(`content`, `img`) " +
                "VALUES ('"+introDuctModelDto.getContent()+"'," +
                " '"+introDuctModelDto.getImg()+"');";
        return JDBC.excuteUpdate(sql);
    }


    public IntroDuctModelDto getIntroDuctModelDto() {
        String sql="select * from hs_introduct order by id desc limit 1" ;
        ResultSet resultSet=JDBC.getResultSet(sql);
       IntroDuctModelDto introDuctModelDto= null;
        try{
            while (resultSet.next()){
                introDuctModelDto= new IntroDuctModelDto();
                //resultSet.getString() 对应数据库的字段名称
                introDuctModelDto.setContent(resultSet.getString("content"));
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBC.close();
        }
        return introDuctModelDto;

    }
    @Override
    public List<IntroDuctModel> getImgList() {
        String sql="select * from hs_introductimg ";
        ResultSet resultSet=JDBC.getResultSet(sql);
        List<IntroDuctModel> list =new ArrayList<>();
        try {
            while (resultSet.next()){
                IntroDuctModel introDuctModel =new IntroDuctModel();
                introDuctModel.setImgs(resultSet.getString("img"));
                list.add(introDuctModel);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBC.close();
        }
        return list;
    }
}
